import axios from 'axios'

// 创建axios实例
const httpInstance = axios.create({
  baseURL: '/api',
  timeout: 5000
})

// axios请求拦截器
httpInstance.interceptors.request.use(config => {
  return config
}, e => Promise.reject(e))

// axios响应式拦截器
// 响应拦截器
httpInstance.interceptors.response.use(
  res => res.data,
  e => {
    console.error('请求错误:', e)
    return Promise.reject(e)
  }
)


export const get = (url, params = {}, config = {}) => {
  return httpInstance.get(url, { params, ...config })
}

export const put = (url, data = {}, config = {}) => {
  return httpInstance.put(url, data, config)
}

export const del = (url, config = {}) => {
  return httpInstance.delete(url, config)
}

// 封装post请求并使用命名导出
export const post = (url, data = {}, config = {}) => {
  return httpInstance.post(url, data, config)
}
